//
//  LoginFromViewController.m
//  Gxj
//
//  Created by 刘伟 on 2017/9/15.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "LoginFromViewController.h"
#import "LoginViewFromView.h"
#import "AppDelegate.h"

@interface LoginFromViewController ()<UITextFieldDelegate>
{
    LoginViewFromView *_loginView;
}

@end

@implementation LoginFromViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self showLoginView];
}

- (void)showLoginView
{
    _loginView = [[LoginViewFromView alloc] init];
    _loginView.userNum.delegate = self;
    _loginView.userPassword.delegate = self;
    self.view = _loginView;
    [_loginView.registerNow addTarget:self action:@selector(clickRegister) forControlEvents:UIControlEventTouchUpInside];
    [_loginView.forgetPassword addTarget:self action:@selector(clickForget) forControlEvents:UIControlEventTouchUpInside];
    [_loginView.login addTarget:self action:@selector(clickLogin) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 点击登陆按钮
- (void)clickLogin
{
    
    if (![_loginView.userNum.text isEqualToString:@""] && ![_loginView.userPassword.text isEqualToString:@""]) {
        
        NSDictionary *dicc = @{@"username":_loginView.userNum.text,
                               @"password":_loginView.userPassword.text
                               };
        
        [KQNetWork postWithURLString:[NSString stringWithFormat:@"%@/api/index/login",MAIN_URL] parameters:dicc success:^(NSDictionary *dic) {
            
            //        [KQUserDefaults writeUserDataWithKey:[dic objectForKey:@"nickname"] forKey:@"nickname"];
            //        [KQUserDefaults writeUserDataWithKey:[dic objectForKey:@"mobile"]   forKey:@"mobile"];
            //        [KQUserDefaults writeUserDataWithKey:[dic objectForKey:@"sex"]      forKey:@"sex"];
            [KQUserDefaults writeUserDataWithKey:[dic objectForKey:@"userid"]   forKey:@"userid"];
            
            NSString * resstate = [[dic objectForKey:@"resstate"] stringValue];
            
            if ([resstate isEqualToString:@"1"]) {
                
                [KQUserDefaults writeUserDataWithKey:_loginView.userNum.text        forKey:@"userName"];
                [KQUserDefaults writeUserDataWithKey:_loginView.userPassword.text   forKey:@"userPassword"];
                [KQUserDefaults writeUserDataWithKey:[[dic objectForKey:@"list"] objectForKey:@"userid"] forKey:@"userid"];
                
                [SVProgressHUD showSuccessWithStatus:[dic objectForKey:@"msg"]];
                [SVProgressHUD displayDurationForString:@"1"];
                [self presentViewController:[CYTabBarController new] animated:YES completion:^{
                    
                }];
            }else{
                [SVProgressHUD showErrorWithStatus:[dic objectForKey:@"msg"]];
            }
            
        } failure:^(NSError *error) {
            
        }];
    }else{
        
        [TTAlertView alertViewWithTitle:@"温馨提示" Message:@"您输入的信息有误" cancelBtnTitle:nil sureBtnTitle:@"确定" sureBtnClick:^{
            
        } cancelBtnClick:^{
            
        }];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.tag == 400000) {
        if (range.location >= 11){
            return NO; // return NO to not change text
        }
    }else if (textField.tag == 400001){
        if (range.location >= 13) {
            return NO;
        }
    }
    
    return YES;
}

#pragma mark - 点击空白回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

#pragma mark - 点击立即注册
- (void)clickRegister
{
    [self presentViewController:[RegisterNowFromViewController new] animated:YES completion:^{
        
    }];
}

#pragma mark - 点击忘记密码
- (void)clickForget
{
    [self.navigationController pushViewController:[ForgetPasswordFromViewController new] animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
